import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

contextBridge.exposeInMainWorld('electronAPI', {
  showOpenDialog: (options) => ipcRenderer.invoke('showOpenDialog', options),
  exportProject: (exportPath) => ipcRenderer.invoke('exportProject', exportPath),
  getSerialPorts: () => ipcRenderer.invoke('get-serial-ports'),
  onSerialPortsUpdate: (callback) => {
    ipcRenderer.on('serial-ports-updated', (event, ports) => callback(ports))
  }
})
// Custom APIs for renderer
const api = {
  // 获取当前工作空间路径
  OnChangeWorkspace: (callback) =>
    ipcRenderer.on('setCurrentWorkSpace', (_event, value) => callback(value)),
  // 获取当前工作空间配置
  OnChangeWorkConfig: (callback) =>
    ipcRenderer.on('setCurrentWorkConfig', (_event, value) => callback(value)),
  //新建工作空间配置文件
  SendNewWorkConfig: () => ipcRenderer.send('newWorkConfig'),
  //打开工作空间配置文件
  SendOpenProject: () => ipcRenderer.send('openProject'),
  //调试
  SendPreview: () => ipcRenderer.send('preview'),
  //获取配置文件内容
  SendLoadConfig: (filePath) => ipcRenderer.sendSync('loadConfig', filePath),
  //保存配置文件内容
  SendSaveConfig: (value) => ipcRenderer.sendSync('saveConfig', value),
  //导出lua脚本
  SendExportLua: () => ipcRenderer.sendSync('exportLua'),
  //导入资源字库
  SendImportFont: () => ipcRenderer.send('importFont'),
  //导入图片
  SendImportImage: () => ipcRenderer.send('importImage'),

  SendImportGif: () => ipcRenderer.send('importGif'),
  //导入音频
  SendImportAudio: () => ipcRenderer.send('importAudio'),
  //导入视频
  SendImportVideo: () => ipcRenderer.send('importVideo'),
  //加载字库展示
  OnLoadFont: (callback) => ipcRenderer.on('loadFont', (_event, value) => callback(value)),
  //加载图片展示
  OnLoadGif: (callback) => ipcRenderer.on('loadGif', (_event, value) => callback(value)),
  OnLoadImage: (callback) => ipcRenderer.on('loadImage', (_event, value) => callback(value)),
  //加载音频展示
  OnLoadAudio: (callback) => ipcRenderer.on('loadAudio', (_event, value) => callback(value)),
  //加载视频展示
  OnLoadVideo: (callback) => ipcRenderer.on('loadVideo', (_event, value) => callback(value)),
  //刷新资源
  SendRefreshResource: () => ipcRenderer.sendSync('refreshResource'),
  //清空资源 type: font,image,audio,video
  SendDeleteResource: (type) => ipcRenderer.send('deleteResource', type),
  //按名称单个删除
  SendDeleteFile: (fileName) => ipcRenderer.sendSync('deleteFile', fileName),
  //主进程调起渲染进程获取设置信息
  OnOpenSettings: (callback) => ipcRenderer.on('openSettings', () => callback()),
  //回调主进程初始化设置
  SendInitSettings: (settings) => ipcRenderer.send('initSettings', settings),
  //新建工程时打开用户目录
  SendOpenUserPath: () => ipcRenderer.sendSync('openUserPath'),
  //新建工程
  OnNewProject: (callback) => ipcRenderer.on('newProject', (_event, value) => callback(value)),
  //下载按钮
  SendDownLoad: () => ipcRenderer.sendSync('downLoad'),
  //输出调试日志
  OnPrintDebugLog: (callback) => ipcRenderer.on('printDebug', (_event, value) => callback(value)),
  //替换图片
  SendRepalcePng: (fileName) => ipcRenderer.send('replacePng', fileName),
  SendRepalceGif: (fileName) => ipcRenderer.send('replaceGif', fileName),
  //页面仿真
  SendPageSimulation: (pageName) => ipcRenderer.send('pageSimulation', pageName)
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    // contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}

// window.addEventListener('DOMContentLoaded', () => {
//   ipcRenderer.on('setCurrentWorkSpace', (_event, value) => {
//     console.log('renderer get current workspace:',value)
//   })
// })
